using UnityEngine;
using System.Collections;

public class Selection : MonoBehaviour 
{
	//private ArrayList controlGroups;
	public ControlGroup currentGroup;
	//public GUIStyle selectionBoxStyle;
	
	private Vector2 originalPos;
	private Vector2 lastPos;
	private bool isDown;
	private bool box;
	
	private RaycastHit hit;
	private Ray ray;
	
	void Start () 
	{
		currentGroup = transform.GetComponent<ControlGroup>();
	}
	
	void Update () 
	{
		
		//Right click
		if(Input.GetMouseButtonDown(1))
		{
			hit = new RaycastHit();
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			
			if(Physics.Raycast(ray, out hit, 100.0f))
			{
				currentGroup.MoveAllUnits(hit.point);
			}
		}
		
		//Left click
		if(Input.GetMouseButtonDown(0))
		{
			originalPos = Input.mousePosition;
			isDown = true;
			box = true;
			
			hit = new RaycastHit();
			ray = Camera.main.ScreenPointToRay(originalPos);
			
			//Selected all friendly units in camera range
			if(Input.GetKey(KeyCode.LeftControl))
			{
			}
			//Add an additionnal unit to the current group
			else if(Input.GetKey(KeyCode.LeftShift)) 
			{
				if(Physics.Raycast(ray, out hit, 100.0f))
				{
					if(hit.collider.gameObject.tag == "Units")
					{
						Unit temp = hit.collider.gameObject.GetComponent<Unit>();
						temp.Selected();
						
						currentGroup.Add(temp);
					}
				}
			}
			//Select a single unit
			else
			{
				if(Physics.Raycast(ray, out hit, 100.0f))
				{
					if(hit.collider.gameObject.tag == "Units")
					{
						currentGroup.Clear();
						
						Unit temp = hit.collider.gameObject.GetComponent<Unit>();
						temp.Selected();
						
						currentGroup.Add(temp);
					}
				}
			}
		}
		//Stop current group
		else if(Input.GetKey(KeyCode.S))
		{
			
		}
		//Hold current group
		else if(Input.GetKey(KeyCode.H))
		{
			
		}
		
		if(isDown)
		{
			lastPos = Input.mousePosition;
			if(Input.GetMouseButtonUp(0))
			{
				isDown=false;
				box = false;
				selectionBox();
			}
		}
	}
	
	private void selectionBox()
	{
		ArrayList units = Game.Instance.getUnits();
		bool selectedOne = false;
		foreach(Unit i in units)
		{
			Vector3 unitPosition = Camera.mainCamera.WorldToScreenPoint(i.transform.position);
			
			if((((unitPosition.x > originalPos.x) && (unitPosition.x < lastPos.x)) || 
				((unitPosition.x < originalPos.x) && (unitPosition.x > lastPos.x))) &&
				(((unitPosition.y > originalPos.y) && (unitPosition.y < lastPos.y)) || 
				((unitPosition.y < originalPos.y) && (unitPosition.y > lastPos.y))))
			{
				if(!selectedOne)
					currentGroup.Clear();
				i.Selected();
				currentGroup.Add(i);
				selectedOne = true;
			}
		}
	}
	
	void OnGUI()
	{
		if(box && (Mathf.Abs((originalPos - lastPos).x)>1.0) && (Mathf.Abs((originalPos - lastPos).y)>1.0))
		{
			//Corner of the box
         	Vector2 origin;
         	origin.x = Mathf.Min(originalPos.x, lastPos.x);
         	origin.y = Mathf.Max(originalPos.y, lastPos.y);
         	origin.y = Screen.height - origin.y; 
         	//Size
         	Vector2 size = originalPos - lastPos;
         	size.x = Mathf.Abs(size.x);
         	size.y = Mathf.Abs(size.y);   
			
         	// Draw with style
         	//GUI.Box(new Rect(origin.x, origin.y, size.x, size.y), "", selectionBoxStyle);
			
			//Draw with default style
			GUI.Box(new Rect(origin.x, origin.y, size.x, size.y), "");
		}
	}
}
